package com.forhappy.light3ds8.datatype;

import java.sql.SQLException;
import java.util.LinkedList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;

import com.forhappy.light3ds8.db.LedDBHelper;

public class DataBasic {
	private static final int IDX_ID = 0;
	private static final int IDX_IDX_DATAS = 1;
	private static final int IDX_IDX_USERNAME = 2;
	private static final int IDX_INFO = 3;

	int id;
	int idx_datas;
	String info;
	String userName;

	public ContentValues toContentValue(){
		ContentValues values = new ContentValues();
		values.put(LedDBHelper.ITEM_ID, id);
		values.put(LedDBHelper.ITEM_IDX_DATAS, idx_datas);
		values.put(LedDBHelper.ITEM_INFO, info);
		values.put(LedDBHelper.ITEM_USERNAME, userName);
		return values;
	}
	public String toJsonStr(){
		JSONObject js = new JSONObject();
		try {
			js.put(LedDBHelper.ITEM_ID, id);
			js.put(LedDBHelper.ITEM_IDX_DATAS, idx_datas);
			js.put(LedDBHelper.ITEM_INFO, info);
			js.put(LedDBHelper.ITEM_USERNAME, userName);
			return js.toString();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	public DataBasic() {

	}
	public DataBasic(JSONObject js) {
		try {
			id = js.getInt(LedDBHelper.ITEM_ID);
			info = js.getString(LedDBHelper.ITEM_INFO);
			idx_datas = js.getInt(LedDBHelper.ITEM_IDX_DATAS);
			userName = js.getString(LedDBHelper.ITEM_USERNAME);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	public DataBasic(
			int id,
			int idx_datas,
			String userName,
			String info){
		this.id = id;
		this.idx_datas = idx_datas;
		this.userName = userName;
		this.info = info;
	}

	@Override
	public String toString() {
		return toJsonStr();
	}

	public static LinkedList<DataBasic> getDatas(Cursor cs){
		LinkedList<DataBasic> loadDatas = new LinkedList<DataBasic>();
		if(null == cs || cs.isAfterLast()){
			return loadDatas;
		}

		try {
			while(cs.moveToNext()){
				DataBasic data = new DataBasic();
				data.id =  cs.getInt(IDX_ID);
				data.idx_datas = cs.getInt(IDX_IDX_DATAS);
				data.userName = cs.getString(IDX_IDX_USERNAME);
				data.info = cs.getString(IDX_INFO);
				loadDatas.add(data);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return loadDatas;
	}
}
